<?php
    class NaturalExtension_PageBuilder {
        private $utils,
            $curr_tab,
            $header_menu_id,
            $footer_menu_id;

        // storage for pages that already exist
        private $created_pages = array();

        private $default_pages = array(
            'starter' => array(
                array(
                    'name'       => 'home',
                    'title'      => 'Home',
                    'content'    => '',
                    'menu_order' => 0
                ),
                array(
                    'name'       => 'author',
                    'title'      => 'About the Author',
                    'content'    => '',
                    'menu_order' => 1
                ),
                array(
                    'name'       => 'buy',
                    'title'      => 'Buy the Book',
                    'content'    => '',
                    'menu_order' => 2
                )
            ),
            'regular' => array(
                array(
                    'name'       => 'home',
                    'title'      => 'Home',
                    'content'    => '',
                    'menu_order' => 0
                ),
                array(
                    'name'       => 'author',
                    'title'      => 'About the Author',
                    'content'    => '',
                    'menu_order' => 1
                ),
                array(
                    'name'       => 'book',
                    'title'      => 'About the Book',
                    'content'    => '[natext_bookshelf]',
                    'menu_order' => 2
                ),
                array(
                    'name'       => 'excerpt',
                    'title'      => 'Book Excerpt',
                    'content'    => '',
                    'menu_order' => 3
                ),
                array(
                    'name'       => 'contact',
                    'title'      => 'Contact the Author',
                    'content'    => '',
                    'menu_order' => 4
                ),
                array(
                    'name'       => 'buy',
                    'title'      => 'Buy the Book',
                    'content'    => '',
                    'menu_order' => 5
                )
            ),
            'advanced' => array(
                array(
                    'name'       => 'home',
                    'title'      => 'Home',
                    'content'    => '',
                    'menu_order' => 0
                ),
                array(
                    'name'       => 'author',
                    'title'      => 'About the Author',
                    'content'    => '',
                    'menu_order' => 1
                ),
                array(
                    'name'       => 'book',
                    'title'      => 'About the Book',
                    'content'    => '[natext_bookshelf]',
                    'menu_order' => 2
                ),
                array(
                    'name'       => 'excerpt',
                    'title'      => 'Book Excerpt',
                    'content'    => '',
                    'menu_order' => 3
                ),
                array(
                    'name'       => 'contact',
                    'title'      => 'Contact the Author',
                    'content'    => '',
                    'menu_order' => 4
                ),
                array(
                    'name'       => 'reviews',
                    'title'      => 'Book Reviews',
                    'content'    => 'Book reviews will be posted on this page.',
                    'menu_order' => 5
                ),
                array(
                    'name'       => 'gallery',
                    'title'      => 'Gallery',
                    'content'    => 'Photos and images will be posted on this page.',
                    'menu_order' => 6
                ),
                array(
                    'name'       => 'trailer',
                    'title'      => 'Book Trailer',
                    'content'    => 'Watch out for this book’s video trailer. COMING SOON!',
                    'menu_order' => 7
                ),
                array(
                    'name'       => 'media-events',
                    'title'      => 'Media & Events',
                    'content'    => 'Book signings, speaking engagements, and other guest appearances by the author will be announced on this page.',
                    'menu_order' => 8
                ),
                array(
                    'name'       => 'blog',
                    'title'      => 'Blog',
                    'content'    => '',
                    'menu_order' => 9
                ),
                array(
                    'name'       => 'other-works',
                    'title'      => 'Other Works',
                    'content'    => 'Other published works or upcoming books by the author will be posted on this page.',
                    'menu_order' => 10
                ),
                array(
                    'name'       => 'buy',
                    'title'      => 'Buy the Book',
                    'content'    => '',
                    'menu_order' => 11
                )
            ),
            'auwebsite' => array(
                array(
                    'name'       => 'home',
                    'title'      => 'Home',
                    'content'    => '[natext_bookshelf]',
                    'menu_order' => 0
                ),
                array(
                    'name'       => 'bio',
                    'title'      => 'Bio',
                    'content'    => '',
                    'menu_order' => 1
                ),
                array(
                    'name'       => 'book',
                    'title'      => 'Book',
                    'content'    => '[natext_bookshelf]',
                    'menu_order' => 2
                ),
                array(
                    'name'       => 'blog',
                    'title'      => 'Blog',
                    'content'    => '',
                    'menu_order' => 3
                ),
                array(
                    'name'       => 'media',
                    'title'      => 'Media',
                    'content'    => '<h2>Photos</h2>[natext_bookshelf_covers]',
                    'menu_order' => 4
                )
            )
        );

        public function __construct() {
            // require_once(OF_FILEPATH . '/patch/inc/Utils.php');

            // create an instance of Utils
            $this->utils = new NaturalExtension_Utils();
            $default_actions = new NaturalExtension_DefaultActions();

            // capture current tab
            $this->curr_tab = isset($_GET['tab']) ? $_GET['tab'] : 'starter';

            // gather pages that already exist and store them inside $created_pages
            foreach (get_pages() as $key => $page) {
                $this->created_pages[$page->post_name] = $page->ID;
            }

            // create default menu items
            $this->header_menu_id = $default_actions->create_menu('header-menu', 'Header Menu');
            $this->footer_menu_id = $default_actions->create_menu('footer-menu', 'Footer Menu');

            // create SMM link category
            $default_actions->create_link_category('Social Media');

            // load specific scripts and styles
            $this->load_scripts_and_styles();
        }

        public function load_scripts_and_styles() {
            $this->utils->nq_scripts(
                array('natext-page-builder', 'natext_page_builder', array('natext'), $this->utils->config['version'])
            );
        }

        /**
         * generates tab per service level
         * @return [HTML] prints the generated HTML tab group
         */
        public function generate_tab_group() {
            $string_builder = array();
            $tabs = array(
                'starter'   => 'Starter',
                'regular'   => 'Regular',
                'advanced'  => 'Advanced',
                'auwebsite' => 'Author Website'
            );

            $string_builder[] = '<div id="icon-themes" class="icon32"><br></div>';
            $string_builder[] = '<h2 class="nav-tab-wrapper">';

            foreach ($tabs as $tab => $name) {
                $class = ($tab == $this->curr_tab) ? ' nav-tab-active ' : '';
                $string_builder[] = sprintf('<a class="nav-tab%s" href="?page=natext&tab=%s">%s</a>', $class, $tab, $name);
            }

            $string_builder[] = '</h2>';

            echo implode('', $string_builder);
        }

        /**
         * generates required form elements per service level
         * @return [HTML] prints the generated HTML table with form elements
         */
        public function generate_service_table() {
            $tab_index = 50;
            $table_headers = array('inc', 'Name', 'Page Title', 'Menu Title', 'Order', 'Content', 'Side Note');
            $table_head = '<thead><tr><th>'
                . implode('</th><th>', $table_headers)
                . '</th></tr></thead>';
            $table_body = array('<tbody class="page-builder-tbody">');

            foreach ($this->default_pages[$this->curr_tab] as $page) {
                $page_exists    = !! in_array($page['name'], array_keys($this->created_pages));
                $elem_disabler  = $page_exists ? 'disabled' : '';
                $elem_checker   = $page_exists ? '' : 'checked';

                if ($page_exists) {
                    $curr_page = get_page($this->created_pages[$page['name']]);
                }

                $page_name      = $page_exists ? $curr_page->post_name : $page['name'];
                $menu_order     = $page_exists ? $curr_page->menu_order : $page['menu_order'];
                $post_content   = $page_exists ? $curr_page->post_content : $page['content'];
                $cell_elements  = array(
                    sprintf('<input %s type="checkbox" tabindex="0" %s class="include-item">', $elem_disabler, $elem_checker),
                    $page_name,
                    sprintf('<input name="post_title" %s type="text" tabindex="%s" value="%s" placeholder="the page title you prefer.">', $elem_disabler, ++$tab_index, $page['title']),
                    sprintf('<input name="menu_title" %s type="text" tabindex="%s" value="%s" placeholder="the menu title you prefer.">', $elem_disabler, ++$tab_index, $page['title']),
                    sprintf('<input name="menu_order" %s type="text" tabindex="%s" value="%s" maxlength="2" placeholder="#" class="menu-order">', $elem_disabler, ++$tab_index, $menu_order),
                    sprintf('<textarea name="post_content" %s tabindex="%s" rows="3" placeholder="add some contents...">%s</textarea>', $elem_disabler, ++$tab_index, $post_content),
                    ($page_exists ? 'This page already exist.' : '')
                );
                $table_row      = '<tr><td>' . implode('</td><td>', $cell_elements) . '</td></tr>';
                $table_body[]   = $table_row;
            }

            $table_body[] = '</tbody>';

            $table_builder = array(
                '<table class="widefat page-builder-table">',
                $table_head,
                implode('', $table_body),
                '</table>'
            );

            echo implode('', $table_builder);
        }

        public function generate_hidden_elements() {
            echo sprintf('<input type="hidden" value="%s" name="header_menu_id">', $this->header_menu_id);
            echo sprintf('<input type="hidden" value="%s" name="footer_menu_id">', $this->footer_menu_id);
            echo sprintf('<input type="hidden" value="%s" name="service_type">', $this->curr_tab);
        }
    }
?>